package com.secsys.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.secsys.dto.BbsPostAuthorDTO;
import com.secsys.dto.BbsPostDTO;
import com.secsys.dto.BbsReplyDTO;
import com.secsys.model.TbBbsModule;
import com.secsys.model.TbBbsPost;
import com.secsys.model.TbSysUser;
import com.secsys.service.ReplyService;
import com.secsys.service.impl.BbsModuleServiceImpl;
import com.secsys.service.impl.PostServiceImpl;
import com.secsys.service.impl.UserServiceImpl;
import com.secsys.util.Constant;
import com.secsys.util.DateFormatUtils;

/**
 * @Description 帖子信息处理Action
 * @author LiusQ
 * @Date 2011-4-9 2:22
 * @version 1.0
 */
public class BbsPostAction extends BaseAction<BbsPostDTO> {

	private BbsModuleServiceImpl bbsModuleService;
	private BbsPostDTO postDTO;
	private PostServiceImpl postService;
    private ReplyService replyService;
	private UserServiceImpl userService;
    private String authorId;
	public String getAuthorId() {
		return authorId;
	}

	public void setAuthorId(String authorId) {
		this.authorId = authorId;
	}

	@Override
	public BbsPostDTO getModel() {
		// TODO Auto-generated method stub
		if (postDTO == null) {
			postDTO = new BbsPostDTO();
		}
		return postDTO;
	}

	public BbsPostDTO getPostDTO() {
		return postDTO;
	}

	/**
	 * @Description 发表帖子
	 * @return
	 * @throws Exception
	 */
	public String publishPost() throws Exception {
		  if(postDTO.getTitle()!=null&&!"".equals(postDTO.getTitle())&&postDTO.getContent()!=null&&!"".equals(postDTO.getContent())){
		TbSysUser userDTO = (TbSysUser) session.get("userDto"); // 获取发帖人Dto
		System.out.println("userID:" + userDTO.getId());
		TbSysUser user = userService.findById(userDTO.getId());
		Date date = new Date(); // 产生一个发布时间
		String publishTime = DateFormatUtils.convertDateTimeToString(date);
	 
		postDTO.setStatue(Constant.POST_AUDITORING); // 设置帖子初始状态位审核中
		TbBbsPost post = postService.postBeanFromDTO(postDTO);
		post.setPublishTime(DateFormatUtils.convertStringToDateTime(publishTime));  //设置发帖时间
		 post.setLastReplyTime(DateFormatUtils.convertStringToDateTime(publishTime)); //设置最后回帖时间
		post.setTbSysUser(user);           //设置发帖用户
		post.setViews(new Integer("1"));   //设置浏览人数
		post.setReplyNum(new Integer("0"));  //设置回复人数
	    post.setLastReplyNo(user.getId());
		TbBbsModule tbBbsModule = bbsModuleService.getModuleByCode(postDTO
				.getCode(), Constant.MODULE_ABLE);
		post.setTbBbsModule(tbBbsModule);
		
		postService.publishPost(post);
		session.put("msg", "恭喜你，帖子发布成功,现在将跳转到用户帖子列表,请稍后...");
		 session.put("url", "<a href='"+Constant.USER_POST_LIST+"authorId="+userDTO.getId()+"'>点击跳转查看我的帖子列表"+"</a>");
		
		
		  }else{   //返回错误信息
			String  uri = ServletActionContext.getRequest().getHeader("referer");
			  session.put("msg", "对不起，帖子发布失败,<font color='red'>标题或内容不能为空</font>,现在将跳转到帖子发布页面,请稍后...");
				 session.put("url", "<a href='"+uri+"'>点击跳返回帖子发布页"+"</a>");
			   
		  }
		  return "publishSuccess";     //设置回复成功后跳转到对应的帖子页面
	}

	/**
	 * @Description 通过id来显示post信息
	 * @return
	 * @throws Exception
	 */
	public String queryPostById() throws Exception {
	 
	     
		TbSysUser userDTO=(TbSysUser)session.get("userDto");  //判断帖子是否被浏览
		if(userDTO!=null){
	    TbBbsPost	post=postService.findById(postDTO.getId());  //获取post持久类
	     post.setViews(post.getViews()+1);  //设置浏览次数，每次响应+1
		 System.out.println(post.getViews()); 
	     postService.updatePost(post);
	     postService.clear();  //清空postService对象中的hibernate session缓存
		}
		BbsPostDTO post = postService.queryPostById(postDTO.getId()); // 获得post
		TbBbsPost bbsPost=new TbBbsPost();
		 bbsPost.setId(postDTO.getId());
		 BbsPostAuthorDTO authorDto=userService.getAuthorInfo(post.getAutorId()); //获取贴主基本信息
		 /**统计此用户的精华贴**/
		  Integer digestCount= postService.getDigestCount(post.getAutorId(), true);
		  authorDto.setDigestNum(digestCount);
	    List<BbsReplyDTO> replyList=replyService.getReplyInfo(bbsPost,postService);		// 基本信息
		session.put("post", post);   //帖子基本信息
		session.put("replyList", replyList);  //该帖子的回复信息
		session.put("authorDto", authorDto); //贴主的基本信息
	//	System.out.println("postDTO.getId():"+postDTO.getId());
		request.put("id", postDTO.getId());
		return "postQuery";
	}

	/**
	 * 
	 * @Description 通过用户查询帖子信息
	 * @return
	 * @throws Exception
	 */
    public String queryPostListByAuthor() throws Exception {
    	   System.out.println(authorId);
           System.out.println(ServletActionContext.getRequest().getRequestURL());
    	   TbSysUser user=new TbSysUser();
    	   user.setId(Integer.parseInt(authorId.trim()));
    	  List<BbsPostDTO> userPostList=postService.queryPostListInfoByAuthor(user);
    	    session.put("userPostList", userPostList);
    	    session.put("titleURL", Constant.MY_TOPIC);   
    	  
    	  return "myPost";
    }
    
    /**
     * 
     * @Description 
     * @return
     * @throws Exception
     */
    public String queryPostListByAuthorPage() throws Exception {
 	   System.out.println(authorId);
        System.out.println(ServletActionContext.getRequest().getRequestURL());
 	   TbSysUser user=new TbSysUser();
 	   user.setId(Integer.parseInt(authorId.trim()));
 	  List<BbsPostDTO> userPostList=postService.queryPostListInfoByAuthor(user);
 	    session.put("userPostList", userPostList);
 	    session.put("titleURL", Constant.MY_TOPIC);   
 	  
 	  return "pageIndex";
 }
    
    /**
     * 
     * @Description  屏蔽帖子操作
     * @return
     * @throws Exception
     */
    public String delPostInfoById()throws Exception{
    	 TbBbsPost post=postService.findById(postDTO.getId());
    	 post.setStatue("屏蔽");
    	        postService.noShowPostCotent(post);
    	        return "queryPost";
    }
    
    
   
	public void setBbsModuleService(BbsModuleServiceImpl bbsModuleService) {
		this.bbsModuleService = bbsModuleService;
	}

	public void setPostDTO(BbsPostDTO postDTO) {
		this.postDTO = postDTO;
	}

	public void setPostService(PostServiceImpl postService) {
		this.postService = postService;
	}
    
	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}
	
	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

}
